# encoding: utf-8
# 把新建的紧急计划刷成已上线

import requests
from app import create_app
from app.modules.integration.models import IntegrationInfo, IntegrationPlan
from app.modules.projectPlan.models import ProjectPlanInfo

_token_id = '62458a5e939c11e8ac9cc81f66c0ef8b'
_plan_ip = '172.16.3.166'


def updateStatus():
    app = create_app(flask_config = 'development')
    with app.app_context():
        # 1 查询紧急计划列表
        for _plan in ProjectPlanInfo.query.filter_by(isEmergency = 0).filter_by(plan_status = 'PLAN_NEW').all():
            onlinePlan(_plan.plan_id)
        pass


def onlinePlan(plan_id):
    app = create_app(flask_config = 'development')
    with app.app_context():
        _url = 'http://' + _plan_ip + ':5003/api/v1/project_plan/toIntegrationPlan?plan_id=' + plan_id + '&token_id=' + _token_id
        _rtn = requests.get(url = _url).content
        print '转测集成 : %s' % _rtn
        _url = 'http://' + _plan_ip + ':5003/api/v1/integration/createEmergencyIntegrationPlan?emergency_plan_id=' + plan_id + '&token_id=' + _token_id
        _rtn = requests.get(url = _url).content
        print '创建紧急集成计划 : %s' % _rtn

        # 获取集成计划id
        _inte = IntegrationPlan.query.filter_by(plan_id = plan_id).first()
        if _inte is None:
            print '集成计划为空'
            return
        _in = IntegrationInfo.query.filter_by(integration_id = _inte.integration_id).first()
        if _inte is None:
            print '集成计划不存在 % ' % _inte.integration_id
            return

        _url = 'http://' + _plan_ip + ':5003/api/v1/integration/handOverIntegration?token_id=' + _token_id + '&integration_id=' + _in.integration_id
        _rtn = requests.get(url = _url).content
        print '交付 : %s' % _rtn
        _url = 'http://' + _plan_ip + ':5003/api/v1/integration/pigeonholeIntegration?token_id=' + _token_id + '&integration_name=' + _in.integration_name
        _rtn = requests.get(url = _url).content
        print '归档 : %s' % _rtn


# onlinePlan('PP201706160520527121710005')
updateStatus()